﻿package edu.pku.se.chocolate.UI.View;

import java.util.ArrayList;

import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
import edu.pku.se.chocolate.ChocolateActivity;
import edu.pku.se.chocolate.R;
import edu.pku.se.chocolate.UI.Adapter.MainAdapter;
import edu.pku.se.chocolate.datastructure.Category;

/**
 * 主界面类，负责主界面的管理实现
 * @author 88b
 *
 */
public class MainView extends ChocolateView{
	
private Handler numHandler = new Handler(new Handler.Callback() {
		
		@Override
		public boolean handleMessage(Message msg) {
			// TODO Auto-generated method stub
			show();
			return true;
		}
	});
	
	private GridView mGrid;
	private final MainAdapter ma = new MainAdapter(ChocolateView.mainActivity);
	private ChocolateView msv = null;
	ArrayList<Category> category = ChocolateActivity.c_solver.getCategoryList();
	private InformationView[] informationView = new InformationView[category.size()];
	public MainView(View self) {
		super(self);
//		for(int i=0;i<category.size();i++)
//			informationView[i] = new InformationView(ChocolateActivity.inflate.inflate(R.layout.informationview, null),category.get(i));
		this.mGrid = (GridView) self.findViewById(R.id.gridView1);
		mGrid.setAdapter(ma);
		mGrid.setBackgroundColor(Color.rgb(51, 51, 51));
		mGrid.setBackgroundResource(R.drawable.bg2);
	}
	
	public void setMainSettingsView(MainSettingsView msv) {
		this.msv = msv;
	}
	
	//设置监听
	public void setListener() {
		mGrid.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position, long id)
			{
				if(ma.getLinkedList().get(position)<category.size()){
					if(informationView[ma.getLinkedList().get(position)] == null)
						informationView[ma.getLinkedList().get(position)] = new InformationView(ChocolateActivity.inflate.inflate(R.layout.informationview, null),category.get(ma.getLinkedList().get(position)));
					informationView[ma.getLinkedList().get(position)].show();
				}
				else
					Toast.makeText(ChocolateView.mainActivity, "未实现", Toast.LENGTH_SHORT).show();
				
			}

			
		});
	}
	
	public MainAdapter getAdapter() {
		return ma;
	}
	
	public void show() {
		mGrid.setAdapter(ma);
		ChocolateActivity.c_solver.setautoReflash(numHandler,ChocolateActivity.c_solver.getAllNotifyTag());
		super.show();
	}
}
